﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NIEN_LUAN
{    
    public partial class FormChiTietLoaiHang : Form
    {
        private NIEN_LUAN.Db.DataContentDataContext db = new NIEN_LUAN.Db.DataContentDataContext();
        private FormQuanLyLoaiHang frmQuanlyloaihang;
        private FormChiTietMatHang frmChitietMatHang;
        private FormTimKiemLoaiHang frmTimKiemLoaiHang;
        private Db.LOAIHANG LoaiHangMuonSua;

        public FormChiTietLoaiHang(FormQuanLyLoaiHang frm)
        {
            frmQuanlyloaihang = frm;
            InitializeComponent();
        }

        public FormChiTietLoaiHang(FormChiTietMatHang frm)
        {
            frmChitietMatHang = frm;            
            InitializeComponent();
        }

        public FormChiTietLoaiHang(Db.LOAIHANG model, FormTimKiemLoaiHang frm)
        {   
            InitializeComponent();
            this.txtMaLH.Text = model.MaLoaiHang;
            this.txtMaLH.Enabled = false;
            this.txtTenLH.Text = model.TenLoaiHang;
            LoaiHangMuonSua = model;
            frmTimKiemLoaiHang = frm;
        }

        public FormChiTietLoaiHang(Db.LOAIHANG model,FormQuanLyLoaiHang frm)
        {
            InitializeComponent();
            this.txtMaLH.Text = model.MaLoaiHang;
            this.txtMaLH.Enabled = false;
            this.txtTenLH.Text = model.TenLoaiHang;
            LoaiHangMuonSua = model;
            frmQuanlyloaihang = frm;
        }

        private void btnThoatCTLH_Click(object sender, EventArgs e)
        {
            this.DialogResult=DialogResult.Cancel;
            this.Close();
        }

        private void btnLuuCTLH_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtMaLH.Text.Trim()))
            {
                MessageBox.Show("Mã loại hàng không được rỗng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(this.txtTenLH.Text.Trim()))
            {
                MessageBox.Show("Tên loại hàng rỗng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtTenLH.Focus();
                return;
            }
          
            var loaihang= new NIEN_LUAN.Db.LOAIHANG
            {
                MaLoaiHang=this.txtMaLH.Text,
                TenLoaiHang=this.txtTenLH.Text
            };

            if (frmQuanlyloaihang != null && LoaiHangMuonSua == null )//them loai hang moi tu bang chi quan ly loai hang
            {
                var lh = (from m in db.LOAIHANGs
                          where m.MaLoaiHang == this.txtMaLH.Text.Trim()
                          select m).ToList();
                if (lh.Count > 0)
                {
                    MessageBox.Show("Mã trùng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                db.LOAIHANGs.InsertOnSubmit(loaihang);
                db.SubmitChanges();
                this.frmQuanlyloaihang.loaihangAdd = loaihang;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }

            if (LoaiHangMuonSua != null && frmQuanlyloaihang != null)//sua chi tiet loai hang 
            {
                ///
                var _loaihang = db.LOAIHANGs.Single(m => m.MaLoaiHang == loaihang.MaLoaiHang);
                _loaihang.MaLoaiHang = loaihang.MaLoaiHang;
                _loaihang.TenLoaiHang = loaihang.TenLoaiHang;
                db.SubmitChanges();
                this.frmQuanlyloaihang.LoaiHangSua = _loaihang;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
           
            if (frmQuanlyloaihang == null && LoaiHangMuonSua == null && frmChitietMatHang != null)//them loai hang moi tu bang chi quan ly loai hang
            {
                var lh = (from m in db.LOAIHANGs
                          where m.MaLoaiHang == this.txtMaLH.Text.Trim()
                          select m).ToList();
                if (lh.Count > 0)
                {
                    MessageBox.Show("Mã trùng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                db.LOAIHANGs.InsertOnSubmit(loaihang);
                db.SubmitChanges();
               
                this.frmChitietMatHang.LoaiHangAdd = loaihang;               
                this.DialogResult = DialogResult.OK;
                this.Close();
            }

            if (LoaiHangMuonSua != null && frmTimKiemLoaiHang != null)
            {
                ///
                var _loaihang = db.LOAIHANGs.Single(m => m.MaLoaiHang == loaihang.MaLoaiHang);
                _loaihang.MaLoaiHang = loaihang.MaLoaiHang;
                _loaihang.TenLoaiHang = loaihang.TenLoaiHang;
                db.SubmitChanges();
                this.frmTimKiemLoaiHang.LoaiHangSua = _loaihang;
                this.DialogResult = DialogResult.OK;
                this.Close();

            }
        }
    }
}
